#include<bits/stdc++.h>
using namespace std;
const int N = 1500+5;
int n,m,v[N],len[N],ton[N];
string t,s[N],out;
double ans = 1;
bool ok(int x,int y)
{
	if(len[y]+x-1>n) return 0;
	for(int i = 1;i<=len[y];i++)
		if(s[y][i]!=t[x+i-1])
			return 0;
	return 1;
}
double qpow(double x,int y)
{
	double res = 1;
	while(y)
	{
		if(y&1) res = res*x;
		y>>=1;
		x = x*x;
	}
	return res;
}
double Sqrt(double x,int y)
{
	if(!y) return 0;
	double l = 0,r = x,res;
	while(r-l>1e-6)
	{
		double mid = (l+r)/2;
		if(qpow(mid,y)<=x) res = mid,l = mid;
		else r = mid;
	}
	return res;
}
double get()
{
	memset(ton,0,sizeof ton);
	int cnt = 0;double res = 1;
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
			if(ok(i,j))
				cnt++,ton[j]++;
	}
	for(int i = 1;i<=m;i++)
		res*=Sqrt(qpow(v[i],ton[i]),cnt);
	return res;
}
void dfs(int x)
{
	if(x>n)
	{
		double now = get();
		if(now>ans) ans = now,out = t;
		return;
	}
	if(t[x]=='.')
	{
		for(int i = '0';i<='9';i++)
			t[x] = i,dfs(x+1);
		t[x] = '.';
	}
	else dfs(x+1);
}
signed main()
{
	freopen("magic.in","r",stdin);
	freopen("magic.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m>>t,t = ' '+t; 
	for(int i = 1;i<=m;i++)
		cin>>s[i]>>v[i],len[i] = s[i].size(),s[i] = ' '+s[i];
	dfs(1);
	for(int i = 1;i<=n;i++)
		cout<<out[i];
	return 0;
}

